# Maintainer: Torsten Keßler <tpkessler at archlinux dot org>
# Contributor: acxz <akashpatel2008 at yahoo dot com>
pkgname=hip-runtime-amd
pkgver=5.7.1
pkgrel=1
pkgdesc="Heterogeneous Interface for Portability ROCm"
arch=('loong64' 'x86_64')
url='https://rocm.docs.amd.com/projects/HIP/en/latest/'
license=('MIT')
depends=('mesa' 'comgr' 'rocminfo' 'rocm-llvm' 'libelf')
makedepends=('cmake' 'python' 'python-cppheaderparser')
provides=('hip')
conflicts=('hip')
# Common HIP dir (AMD or nVidia)
_hip='https://github.com/ROCm-Developer-Tools/HIP'
# ROCclr, HIPAMD and OpenCl repositories
_clr='https://github.com/ROCm-Developer-Tools/clr'
# Binary hipcc compiler (previously a perl script)
_hipcc='https://github.com/ROCm-Developer-Tools/HIPCC'
source=("$pkgname-$pkgver.tar.gz::$_hip/archive/rocm-$pkgver.tar.gz"
        "$pkgname-clr-$pkgver.tar.gz::$_clr/archive/rocm-$pkgver.tar.gz"
        "$pkgname-hipcc-$pkgver.tar.gz::$_hipcc/archive/rocm-$pkgver.tar.gz")
sha256sums=('eaa0e14a9ae45c58ed37863797b683a7778b3cbbf92f5b6529ec65fd61d61f3e'
            'c78490335233a11b4d8a5426ace7417c555f5e2325de10422df06c0f0f00f7eb'
            'd47d27ef2b5de7f49cdfd8547832ac9b437a32e6fc6f0e9c1646f4b704c90aee')
_dirhip="$(basename "$_hip")-$(basename "${source[0]}" ".tar.gz")"
_dirclr="$(basename "$_clr")-$(basename "${source[1]}" ".tar.gz")"
_dirhipcc="$(basename "$_hipcc")-$(basename "${source[2]}" ".tar.gz")"

build() {
  local hipcc_args=(
    -Wno-dev
    -S "$srcdir/$_dirhipcc"
    -B build-hipcc
    -DCMAKE_BUILD_TYPE=None
  )
  cmake "${hipcc_args[@]}"
  cmake --build build-hipcc

  local hip_args=(
    -Wno-dev
    -S "$srcdir/$_dirclr"
    -B build
    -DCMAKE_BUILD_TYPE=None
    -DCMAKE_INSTALL_PREFIX=/opt/rocm/
    -DHIP_COMMON_DIR="$srcdir/$_dirhip"
    -DHIPCC_BIN_DIR="$srcdir/build-hipcc"
    -DHIP_CATCH_TEST=0
    -DCLR_BUILD_HIP=ON
    -DCLR_BUILD_OCL=OFF
  )
  cmake "${hip_args[@]}"
  cmake --build build
}

package() {
  DESTDIR="$pkgdir" cmake --install build
  # Binary hipcc and hipconfig currently don't work, so we don't install them:
  # https://github.com/ROCm-Developer-Tools/HIPCC/issues/103
  #
  # There's no install target for hipcc and hipconfig, so copy them manually
  # install "$srcdir/build-hipcc/hipcc.bin" "$pkgdir/opt/rocm/bin/hipcc.bin"
  # install "$srcdir/build-hipcc/hipconfig.bin" "$pkgdir/opt/rocm/bin/hipconfig.bin"

  # Remove Windows scripts
  rm "$pkgdir/opt/rocm/bin/hipcc.bat"
  rm "$pkgdir/opt/rocm/bin/hipconfig.bat"

  install -Dm644 "$srcdir/$_dirhip/LICENSE.txt" "$pkgdir/usr/share/licenses/$pkgname/LICENSE"
  echo '/opt/rocm/hip/lib' > "$pkgname.conf"
  install -Dm644 "$pkgname.conf" "$pkgdir/etc/ld.so.conf.d/$pkgname.conf"
}
